﻿// Decompiled with JetBrains decompiler
// Type: MCM.Common.LocalizationUtils
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using HarmonyLib.BUTR.Extensions;
using System;
using System.Collections.Generic;

#nullable enable
namespace MCM.Common
{
  public static class LocalizationUtils
  {
    public static string Localize(string rawText, Dictionary<string, object>? attributes = null)
    {
      return LocalizationUtils.TextObjectWrapper.Create(rawText, attributes).Localize();
    }

    private readonly ref struct TextObjectWrapper
    {
      private static readonly LocalizationUtils.TextObjectWrapper.TextObjectCtorDelegate? _textObjectCtor = AccessTools2.GetDeclaredConstructorDelegate<LocalizationUtils.TextObjectWrapper.TextObjectCtorDelegate>("TaleWorlds.Localization.TextObject", new Type[2]
      {
        typeof (string),
        typeof (Dictionary<string, object>)
      });
      private static readonly LocalizationUtils.TextObjectWrapper.ToStringDelegate? _toString = AccessTools2.GetDeclaredDelegate<LocalizationUtils.TextObjectWrapper.ToStringDelegate>("TaleWorlds.Localization.TextObject:ToString", Type.EmptyTypes);
      private readonly object? _object;

      public static LocalizationUtils.TextObjectWrapper Create(
        string rawText,
        Dictionary<string, object>? attributes = null)
      {
        LocalizationUtils.TextObjectWrapper.TextObjectCtorDelegate textObjectCtor = LocalizationUtils.TextObjectWrapper._textObjectCtor;
        return new LocalizationUtils.TextObjectWrapper(textObjectCtor != null ? textObjectCtor(rawText, attributes) : (object) null);
      }

      private TextObjectWrapper(object? @object) => this._object = @object;

      public string Localize()
      {
        return this._object == null || LocalizationUtils.TextObjectWrapper._toString == null ? string.Empty : LocalizationUtils.TextObjectWrapper._toString(this._object) ?? string.Empty;
      }

      private delegate object TextObjectCtorDelegate(
        string rawText,
        Dictionary<string, object>? attributes = null);

      private delegate string? ToStringDelegate(object instance);
    }
  }
}
